package com.itfitness.stretchcalendarview.widget.calendarview.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.hjq.shape.layout.ShapeFrameLayout;
import com.itfitness.stretchcalendarview.R;
import com.itfitness.stretchcalendarview.widget.calendarview.bean.CalendarDateBean;
import com.itfitness.stretchcalendarview.widget.calendarview.widget.StretchCalendarView.OnDateSelectCallBack;
import com.zhaoxing.view.sharpview.SharpTextView;

import java.util.List;


public class StretchCalendarAdapter extends BaseAdapter {
    private List<CalendarDateBean> mData;
    private Context mContext;
    private int mSelectYear = 0;
    private int mSelectMonth = 0;
    private int mSelectDay = 0;
    private OnDateSelectCallBack onDateSelectCallBack;
    public StretchCalendarAdapter(int selectYear,int selectMonth,int selectDay,List<CalendarDateBean> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
        this.mSelectYear = selectYear;
        this.mSelectMonth = selectMonth;
        this.mSelectDay = selectDay;
    }

    public OnDateSelectCallBack getOnDateSelectCallBack() {
        return onDateSelectCallBack;
    }

    public void setOnDateSelectCallBack(OnDateSelectCallBack onDateSelectCallBack) {
        this.onDateSelectCallBack = onDateSelectCallBack;
    }

    public List<CalendarDateBean> getData() {
        return mData;
    }

    public void setData(List<CalendarDateBean> mDatas) {
        this.mData = mDatas;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if(convertView == null){
            convertView = View.inflate(mContext, R.layout.item_stretch_calendar, null);
            viewHolder = new ViewHolder();
            viewHolder.tvDate = convertView.findViewById(R.id.tvDate);
            viewHolder.flItemContainer = convertView.findViewById(R.id.flItemContainer);
            viewHolder.tvLunar = (TextView) convertView.findViewById(R.id.tvLunar);
            viewHolder.srtvBg = (SharpTextView) convertView.findViewById(R.id.srtv_bg);
            viewHolder.sflBg = (ShapeFrameLayout) convertView.findViewById(R.id.sfl_bg);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        CalendarDateBean date = mData.get(i);
        if(date.getDay() != 0){
            viewHolder.tvDate.setText("" + date.getDay());
            viewHolder.flItemContainer.setVisibility(View.VISIBLE);
        }else {
            viewHolder.tvDate.setText("");
            viewHolder.flItemContainer.setVisibility(View.GONE);
        }
        if(TextUtils.isEmpty(date.getFestival())){
            viewHolder.tvLunar.setText(date.getLunar());
        }else {
            viewHolder.tvLunar.setText(date.getFestival());
        }
        //选中效果
        if(date.getDay() != 0 && date.getYear() == mSelectYear
                && date.getMonth() == mSelectMonth
                && date.getDay() == mSelectDay){
            viewHolder.tvDate.setTextColor(Color.WHITE);
            viewHolder.tvLunar.setTextColor(Color.WHITE);
            viewHolder.srtvBg.setVisibility(View.VISIBLE);
            viewHolder.sflBg.getShapeDrawableBuilder().setSolidColor(Color.parseColor("#04C26F")).intoBackground();
//            viewHolder.flItemContainer.setBackground(mContext.getResources().getDrawable(R.drawable.shape_selectdate_bg));
        }else {
            viewHolder.tvDate.setTextColor(Color.BLACK);
            viewHolder.tvLunar.setTextColor(Color.parseColor("#808080"));
            viewHolder.srtvBg.setVisibility(View.INVISIBLE);
            viewHolder.sflBg.getShapeDrawableBuilder().setSolidColor(Color.parseColor("#F5F5F7")).intoBackground();
//            viewHolder.flItemContainer.setBackground(null);
        }
        convertView.setOnClickListener(v -> {
            if (onDateSelectCallBack != null){
                mSelectYear = date.getYear();
                mSelectMonth = date.getMonth();
                mSelectDay = date.getDay();
                onDateSelectCallBack.onDateSelect(date.getYear(),date.getMonth() + 1,date.getDay());
                notifyDataSetChanged();
            }
        });
        return convertView;
    }
    static class ViewHolder{
        public TextView tvDate;
        public FrameLayout flItemContainer;
        public TextView tvLunar;
        public SharpTextView srtvBg;
        public ShapeFrameLayout sflBg;
    }
}
